import { defineStore } from 'pinia';
import {
  DelDictionary,
  GetDictionary,
  AddOrEditDictionary,
} from '@/api/dictionary';

//字典列表
export const dictionaryList = [
  {
    code: '工作方式',
    fieldType: 'string',
    type: 1,
  },
  {
    code: '工序类别',
    fieldType: 'string',
    type: 2,
  },
];

export const useDictionaryStore = defineStore('dictionary', {
  state: () => ({
    dictionary: {}, //字典对象
  }),
  actions: {
    async getOption(type: number) {
      let res = await GetDictionary({
        type,
      });
      this.dictionary[type] =
        res?.map((item: any) => ({
          ...item,
          id:
            dictionaryList.find((item) => type === item.type)?.fieldType ===
            'string'
              ? item.id + ''
              : item.id,
        })) || [];
    },
    async addOrEdit(formData: any) {
      await AddOrEditDictionary({
        ...formData,
        id: Number(formData.id),
      });
      this.getOption(formData.type);
    },
    async del(id: any, type: number) {
      await DelDictionary({ id });
      this.getOption(type);
    },
  },
});
